package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.CollarmxDao;
import io.renren.dto.CollarmxDTO;
import io.renren.dto.FoodmxDTO;
import io.renren.entity.CollarEntity;
import io.renren.entity.CollarmxEntity;
import io.renren.service.CollarService;
import io.renren.service.CollarmxService;
import io.renren.service.FoodmxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 辅材领用明细表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class CollarmxServiceImpl extends CrudServiceImpl<CollarmxDao, CollarmxEntity, CollarmxDTO> implements CollarmxService {
     @Autowired
     private CollarmxDao collarmxDao;
     @Autowired
     private CollarmxService collarmxService;
     @Autowired
     private CollarService collarService;
     @Autowired
     private FoodmxService foodmxService;

    @Override
    public QueryWrapper<CollarmxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CollarmxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public List<CollarmxDTO> seleCollarmx(Long collarId) {
        return collarmxDao.seleCollarmx(collarId);
    }

    @Override
    public void deleteOrder(Long order) {
        collarmxDao.deleteOrder(order);
    }

    @Override
    public HashMap details(long collarId,String type) {
        HashMap  map = new HashMap();
        CollarEntity collarEntity = collarService.selectById(collarId);
        map.put("collar",collarEntity);
        List<CollarmxDTO> list = collarmxService.seleCollarmx(collarId);
        ArrayList<CollarmxDTO> collarmxList = new ArrayList<>();
        for (CollarmxDTO collarmxDTO:list ) {
            collarmxDTO.setPurchaseNum(collarmxDTO.getCollarNum());
            if(1 == collarEntity.getStatus()){
                FoodmxDTO totaoNum = foodmxService.getTotaoNum(collarmxDTO.getFoodId(), collarEntity.getCanteenId());
                collarmxDTO.setTotalNum(totaoNum.getTotaoNumber());
            }
          collarmxList.add(collarmxDTO);
        }
        map.put("collarMx",collarmxList);
        System.out.println(map);
        return map;
    }

    @Override
    public void updateOrder2(long orderId, long foodId, float purchaseNum) {
        collarmxDao.updateOrder2(orderId,foodId,purchaseNum);
    }

    @Override
    public void delectOrder(long orderId, long foodId) {
        collarmxDao.delectOrder(orderId,foodId);
    }

    @Override
    public CollarmxDTO getOrder(long orderId, long foodId) {
        CollarmxDTO order = collarmxDao.getOrder(orderId, foodId);
        return order;
    }

}